home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Franz PD / Franz PD Disk #001 (19xx)(Amiga User Group Deutschland e.V.).zip / Franz PD Disk #001 (19xx)(Amiga User Group Deutschland e.V.).adf / SuperBitMap / super.c < prev    next >
C/C++ Source or Header  |  1986-10-22  |  9KB  |  325 lines

  1. /*  Super.c - superbitmap scrolling and printing demo - Requires 1.2   */
  2. /*                  phillip lindsay / carolyn scheppner                */
  3. /*                           CBM 06/30/86                              */
  4.  
  5. #include "exec/types.h"
  6. #include "exec/execbase.h"
  7. #include "exec/exec.h"
  8. #include "graphics/gfxbase.h"
  9. #include "graphics/rastport.h"
  10. #include "graphics/layers.h"
  11. #include "intuition/intuition.h"
  12.  
  13. #include "devices/printer.h"
  14.  
  15. /* constants */
  16.  
  17. #define SBWIDTH    800
  18. #define SBHEIGHT   400
  19.  
  20. #define WIDTH      640       
  21. #define HEIGHT     200
  22. #define MAXWIDTH  WIDTH
  23. #define MAXHEIGHT HEIGHT
  24. #define MINWIDTH  (MAXWIDTH/16)
  25. #define MINHEIGHT (MAXHEIGHT/8)
  26. #define DEPTH   2              /* bitplanes                        */
  27. #define COLORS  4              /* COLORS = pow(DEPTH,2);           */
  28. #define NORMAL  -1             /* value used for normal Abort()    */
  29.  
  30. /* functions we want to declare */
  31.  
  32. long   OpenLibrary();
  33. struct IntuiMessage *GetMsg();
  34. struct Window *OpenWindow();
  35.  
  36. /* external I want to see */ 
  37.  
  38. extern long SysBase;
  39.  
  40. /* working base pointers - pre-initialized for simplified aborting */
  41.  
  42. struct GfxBase *GfxBase = 0;
  43. struct IntuitionBase *IntuitionBase = 0;
  44.  
  45. /* our window structures...*/
  46.  
  47. struct NewWindow mynuwindow = 
  48.  {
  49.   0,0,WIDTH,HEIGHT,
  50.   0,1,
  51.   CLOSEWINDOW | VANILLAKEY,
  52.   GIMMEZEROZERO | SUPER_BITMAP | WINDOWDEPTH   | WINDOWCLOSE |
  53.   WINDOWSIZING  | WINDOWDRAG   | ACTIVATE,
  54.   NULL,NULL,
  55.   "1.2 SuperBitMap Demo - SPACE=Draw/Pause UDLR=Scroll P=Print Q=Quit",
  56.   NULL,
  57.   NULL,
  58.   MINWIDTH,MINHEIGHT,
  59.   MAXWIDTH,MAXHEIGHT,
  60.   WBENCHSCREEN
  61.  };       
  62.  
  63. /* Window related structures */
  64. struct Window   *mywindow;
  65. struct ViewPort *vp;
  66. struct RastPort *rp;
  67. struct BitMap   bitmap;
  68.  
  69. /* Intuition IDCMP stuff */
  70. struct IntuiMessage *msg;
  71. ULONG  class;
  72. USHORT code;
  73.  
  74. /* Layer stuff */
  75. ULONG  LayersBase;
  76. struct Layer     *l;
  77. struct Layer_Info *li;
  78.  
  79.  
  80. /**************************************************************************/ 
  81. main()
  82. {
  83.  
  84. long count,          /* misc. loop var. */
  85.      gapX, gapY,     /* holds current val for the line gap */
  86.      cycle,          /* this is the current color register being used */
  87.      x,y;            /* current line dest. coor. */
  88.  
  89. UBYTE do_close;       /* this flag is TRUE for window close gadget selected */
  90. UBYTE freeze;         /* flag to freeze display */
  91. int   error;          /* for return value of superDump() */
  92. int   deltaX, deltaY; /* scroll deltas */
  93. int   k;
  94.  
  95.  /* Initialize PLANEPTR's to ZERO for simple deallocating */
  96.  for(k=0; k<DEPTH; k++)  bitmap.Planes[k] = NULL;
  97.  
  98.  GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0);
  99.  Abort(GfxBase,"Can't open Graphics library");
  100.  
  101.  IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0); 
  102.  Abort(IntuitionBase,"Can't open Intuition library");
  103.  
  104.  LayersBase = OpenLibrary("layers.library",0);
  105.  Abort(LayersBase,"Can't open Layers library");
  106.  
  107.  /* let's get our bitplanes */
  108.  
  109.  InitBitMap(&bitmap,DEPTH,SBWIDTH,SBHEIGHT);
  110.  for(count=0;count < DEPTH;count++)
  111.    if((bitmap.Planes[count] = (PLANEPTR) AllocRaster(SBWIDTH,SBHEIGHT)) == NULL)
  112.      Abort(NULL,"No memory for bitplanes.");
  113.    else
  114.      BltClear(bitmap.Planes[count],RASSIZE(SBWIDTH,SBHEIGHT),NULL);
  115.  
  116.  mynuwindow.BitMap = &bitmap; /* tell mynuwindow where our bitmap is */
  117.  
  118.  Abort((mywindow = OpenWindow(&mynuwindow)),"Can't open window");             
  119.  
  120.  rp = mywindow->RPort;
  121.  vp = (struct ViewPort *) ViewPortAddress(mywindow);
  122.  l  = rp->Layer;
  123.  li = l->LayerInfo;
  124.  deltaX = 4;
  125.  deltaY = 2;
  126.  
  127.  SetDrMd(rp,JAM1);
  128.  
  129.  
  130.  /* Initialize flags to defaults */
  131.  
  132.  do_close=0x00; 
  133.  freeze = FALSE;
  134.  
  135.  /* Loop until close gadget hit */
  136.  
  137.  while(!do_close)
  138.   {
  139.   if(!freeze)
  140.      {
  141.      x = RangeRand(SBWIDTH);
  142.      y = RangeRand(SBHEIGHT);
  143.      gapX =  RangeRand(4)+2;
  144.      gapY =  RangeRand(4)+2;
  145.      cycle = RangeRand(4);
  146.      SetAPen(rp,cycle);
  147.  
  148.      for (; ((x < SBWIDTH)&&(y > 0)); x += gapX, y -= gapY)
  149.         {
  150.         Move(rp,0,y);
  151.         Draw(rp,x,0);
  152.         Move(rp,SBWIDTH-1-x,0);
  153.         Draw(rp,SBWIDTH-1,y);
  154.         Move(rp,SBWIDTH-1,SBHEIGHT-1-y);
  155.         Draw(rp,SBWIDTH-1-x,SBHEIGHT-1);
  156.         Move(rp,x,SBHEIGHT-1);
  157.         Draw(rp,0,SBHEIGHT-1-y);
  158.         }
  159.      }
  160.  
  161.   while(msg=(struct IntuiMessage *)GetMsg(mywindow->UserPort))
  162.      {
  163.      class = msg->Class;
  164.      code =  msg->Code;
  165.      ReplyMsg(msg);
  166.      switch(class)
  167.         {
  168.         case CLOSEWINDOW:
  169.            do_close = TRUE;
  170.            break;
  171.         case VANILLAKEY:
  172.            switch(code)
  173.               {
  174.               case 'u':
  175.                  if((l->Scroll_Y + deltaY) < maxYScroll(mywindow) )
  176.                     ScrollLayer(li,l,0,deltaY);
  177.                  break;
  178.               case 'd':
  179.                  if((l->Scroll_Y - deltaY) > 0 )
  180.                     ScrollLayer(li,l,0,-deltaY);
  181.                  break;
  182.               case 'l':
  183.                  if((l->Scroll_X + deltaX) < maxXScroll(mywindow) )
  184.                     ScrollLayer(li,l,deltaX,0);
  185.                  break;
  186.               case 'r':
  187.                  if((l->Scroll_X-deltaX) > 0 )
  188.                     ScrollLayer(li,l,-deltaX,0);
  189.                  break;
  190.               case ' ':
  191.                  freeze = (freeze == TRUE) ? FALSE : TRUE;
  192.                  break;
  193.               case 'p':
  194.                  /* ScreenDump of SuperBitMap */
  195.                  error = superDump(rp,vp);
  196.                  if (error)  printf("DumpRPort error = %ld\n",error);
  197.                  break;
  198.               case 'q':
  199.                  do_close = TRUE;
  200.                  break;
  201.               }
  202.         }
  203.      }
  204.   }
  205.  
  206.  
  207.  Abort(NORMAL,"\nGoodbye..");
  208. }
  209.  
  210.  
  211.  
  212. /* Abort - an easy going exit routine - if val <= 0 a clean-up and exit will */ 
  213. /*     take place...val == 0 implies ERROR...val == -1 implies normal exit   */
  214. /*     and val >= 1 implies no exit or error                                 */
  215.  
  216. Abort(val,str)
  217. long val;
  218. UBYTE *str;
  219. {
  220.  long count;
  221.  
  222.  if(val < 1)
  223.    {
  224.     puts(str);
  225.     if(mywindow) msgflush(),CloseWindow(mywindow);
  226.     if(bitmap.Planes[0] != NULL)
  227.       for(count=0;(bitmap.Planes[count] != NULL) && (count < 8);count++)
  228.         FreeRaster(bitmap.Planes[count],SBWIDTH,SBHEIGHT);
  229.     if(GfxBase) CloseLibrary(GfxBase);
  230.     if(IntuitionBase) CloseLibrary(IntuitionBase);
  231.     if(LayersBase) CloseLibrary(LayersBase);
  232.     exit(val == 0 ? 1 : 0);
  233.    }
  234. }
  235.  
  236.  
  237. /* msgflush - get/reply to all messages */
  238. msgflush()
  239. {
  240. while((msg = GetMsg(mywindow->UserPort))) ReplyMsg(msg);
  241. }
  242.  
  243.  
  244. int maxXScroll(w)
  245. struct Window *w;
  246. {
  247. return(SBWIDTH - (w->Width - w->BorderLeft - w->BorderRight));
  248.  
  249. int maxYScroll(w)
  250. struct Window *w;
  251. {
  252. return(SBHEIGHT - (w->Height - w->BorderTop - w->BorderBottom));
  253.  
  254.  
  255. /***************************/
  256. /* SuperBitmap Screen Dump */
  257. /***************************/
  258. superDump(rp,vp)
  259.    struct RastPort *rp;
  260.    struct ViewPort *vp;
  261.    {
  262.    /* printer stuff */
  263.    union printerIO {
  264.        struct IOStdReq ios;
  265.        struct IODRPReq iodrp;
  266.        struct IOPrtCmdReq iopc;
  267.        };
  268.    union printerIO         *request;
  269.    extern struct IORequest *CreateExtIO();
  270.    extern struct MsgPort   *CreatePort();
  271.  
  272.    struct MsgPort  *printerPort;
  273.    struct RastPort dRastPort;    /* Dummy RastPort */
  274.    int error;
  275.  
  276.    /* Lock the Layer                            */
  277.    /* Update the SuperBitmap                    */
  278.    /* Copy window's Rastport structure to dummy */
  279.    /* Insert ptr to our SuperBitmap             */
  280.    /* NULL the Layer pointer                    */
  281.    /* Unlock the Layer                          */
  282.    LockLayerRom(rp->Layer);
  283.    SyncSBitMap(rp->Layer);
  284.    dRastPort = *rp;
  285.    dRastPort.BitMap = rp->Layer->SuperBitMap;
  286.    dRastPort.Layer  = NULL;
  287.    UnlockLayerRom(rp->Layer);
  288.  
  289.    printerPort = CreatePort("my.print.port",0);
  290.    request =  (union printerIO *)CreateExtIO(printerPort,
  291.                                         sizeof(union printerIO));
  292.  
  293.    error = OpenDevice("printer.device",0,request,0);
  294.    if(error != 0) goto cleanup2;
  295.  
  296.    request->iodrp.io_Command = PRD_DUMPRPORT;
  297.    request->iodrp.io_RastPort = &dRastPort;
  298.    request->iodrp.io_ColorMap = vp->ColorMap;
  299.    request->iodrp.io_Modes = (ULONG)vp->Modes;
  300. /*   request->iodrp.io_SrcX = 0;     MEMF_CLEAR zeroed this  */
  301. /*   request->iodrp.io_SrcY = 0;     MEMF_CLEAR zeroed this  */
  302.    request->iodrp.io_SrcWidth = SBWIDTH;
  303.    request->iodrp.io_SrcHeight = SBHEIGHT;
  304.    request->iodrp.io_DestCols = SBWIDTH;
  305. /*   request->iodrp.io_DestRows = 0; MEMF_CLEAR zeroed this  */
  306.    request->iodrp.io_Special = SPECIAL_ASPECT;
  307.  
  308.    error = DoIO(request);
  309.  
  310.    CloseDevice(request);
  311.  
  312.  
  313. cleanup2:
  314.    DeleteExtIO(request, sizeof(union printerIO));
  315.    DeletePort(printerPort);
  316. cleanup1:
  317. cleanup0:
  318.    return(error);
  319. }               /* end of demo screen dump */
  320.  
  321.  
  322. /* EOF */
  323.